package com.lxr.test5002.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;

import com.lxr.test5002.base.App;
import com.lxr.test5002.base.Constants;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MyUtil {
    /**
     * 获取16进制随机数
     *
     * @param len
     * @return
     */
    public static String randomHexString(int len) {
        try {
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < len; i++) {
                result.append(Integer.toHexString(new Random().nextInt(16)));
            }
            return result.toString().toUpperCase();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 方法描述:[打开或关闭飞行模式]
      * @param isEnable  1 关闭  0打开
     * @author: LXR-zsm
     * @Date: 2023/12/19
     * @Return: void
     */
    public static void setLtcPowerReceiver(int isEnable){
        Intent var1 = new Intent("com.android.action.ltc.mobileData");
        var1.putExtra("enable", isEnable);
        var1.setComponent(new ComponentName("com.android.settings", "com.android.settings.kby.LtcPowerReceiver"));
        App.getContext().sendBroadcast(var1);
    }

    public static boolean isValueEmpty(String str) {
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }

    /**
     * 方法描述:[调整音量]
     *
     * @param
     * @author: LXR-zsm
     * @Date: 2023/12/18
     * @Return: void
     */
    public static void setAudioManager() {
        AudioManager audiomanage = (AudioManager) App.getContext().getSystemService(Context.AUDIO_SERVICE);
        List<Integer> listType = new ArrayList<>();
        listType.add(AudioManager.STREAM_MUSIC); //手机音乐的声音
        listType.add(AudioManager.STREAM_RING); //电话铃声的声音
        listType.add(AudioManager.STREAM_SYSTEM); //手机系统的声音
        listType.add(AudioManager.STREAM_ALARM); //手机闹铃的声音
        for (int type : listType) {
            Log.i(Constants.LOG_TAG, "类型：" + type + ",当前音量：" + audiomanage.getStreamVolume(type) + ",最大音量：" + audiomanage.getStreamMaxVolume(type));
            audiomanage.setStreamVolume(type, audiomanage.getStreamMaxVolume(type) / 3, AudioManager.FLAG_SHOW_UI);
            Log.i(Constants.LOG_TAG, "修改后类型：" + type + ",当前音量：" + audiomanage.getStreamVolume(type) + ",最大音量：" + audiomanage.getStreamMaxVolume(type));
        }
    }

    private static final String HEX_STR = "0123456789ABCDEF";

    /**
     * 16进制字符串转字节数据
     *
     * @param hexStr 字符串
     * @return 字节数组
     */
    public static byte[] hexToByteArr(String hexStr) {
        char[] charArr = hexStr.toCharArray();
        byte[] btArr = new byte[charArr.length / 2];
        int index = 0;
        for (int i = 0; i < charArr.length; i++) {
            int highBit = HEX_STR.indexOf(charArr[i]);
            int lowBit = HEX_STR.indexOf(charArr[++i]);
            btArr[index] = (byte) (highBit << 4 | lowBit);
            index++;
        }
        return btArr;
    }


    public static String bytesToString(byte[] bytes) {
        final char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[bytes.length * 2];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            hexChars[i * 2] = hexArray[v >>> 4];
            hexChars[i * 2 + 1] = hexArray[v & 0x0F];

            sb.append(hexChars[i * 2]);
            sb.append(hexChars[i * 2 + 1]);
            sb.append(' ');
        }
        return sb.toString();
    }

    /**
     * 将byte数组转为16进制字符串
     *
     * @param bArr byte数组
     * @return 16进制字符串
     */
    public static String bytesToHexString(byte[] bArr) {
        StringBuffer sb = new StringBuffer(bArr.length);
        String sTmp;
        for (int i = 0; i < bArr.length; i++) {
            sTmp = Integer.toHexString(0xFF & bArr[i]);
            if (sTmp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTmp.toUpperCase());
        }
        return sb.toString();
    }
}
